package com.yztctech.a25_01_startservice;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * Created by bodhixu on 2016/9/24.
 * 自定义Service
 */
public class MyService2 extends Service{

    //创建，只会触发一次
    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("Tag", "----------> onCreate");
    }

    //启动，会被触发多次;对于bindService，它不会触发的
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("Tag", "----------> onStartCommand");
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);//2s后自己结束
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //todo Service结束自身
                stopSelf();
            }
        }).start();
        return super.onStartCommand(intent, flags, startId);
    }

    //销毁，只会被触发一次
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("Tag", "----------> onDestroy");
    }

    //必须要实现，但是在startService启动方式下，不会被触发
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


}
